using System;
using System.Linq;
using System.Collections.Generic;
using Managistics.Model;

namespace Managistics.Core.Repositories.Impl
{
    public class PurchaseOrderReportRepository : RepositoryBase<PurchaseOrderReport, double>, IPurchaseOrderReportRepository
    {
        /// <summary>
        /// Get all purchases ordered detail
        /// </summary>
        /// <param name="reportType"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="supplierCode"></param>
        /// <param name="goodCode"></param>
        /// <returns></returns>
        public IList<PurchaseOrderReport> GetPurchaseOrderedDetailByGoodOrSupplier(int reportType, DateTime startDate, DateTime endDate, string supplierCode, string goodCode)
        {
            var query = this.Session.QueryOver<PurchaseOrderReport>()
                            .WhereRestrictionOn(p => p.OrderDate).IsBetween(startDate).And(endDate);
            // Good
            if (reportType == 1)
            {
                query = query
                    .OrderBy(p => p.ItemId).Asc
                    .ThenBy(p => p.OrderDate).Asc
                    .ThenBy(p => p.OrderId).Asc;
            }
            else // Supplier
            {
                query = query
                    .OrderBy(p => p.Supplierid).Asc
                    .ThenBy(p => p.OrderDate).Asc
                    .ThenBy(p => p.OrderId).Asc
                    .ThenBy(p => p.ItemId).Asc;
            }

            if (!string.IsNullOrEmpty(supplierCode))
            {
                query = query.Where(p => p.Supplierid == supplierCode);
            }

            if (!string.IsNullOrEmpty(goodCode))
            {
                query = query.Where(p => p.ItemId == goodCode);
            }

            return query.List<PurchaseOrderReport>();
        }

        /// <summary>
        /// Get all purchse inputed detail
        /// </summary>
        /// <param name="reportType"></param>
        /// <param name="intputType"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="supplierCode"></param>
        /// <param name="goodCode"></param>
        /// <returns></returns>
        public IList<PurchaseInputReport> GetPurchaseInputedDetailByGoodOrSupplier(int reportType, DateTime startDate, DateTime endDate, string supplierCode, string goodCode)
        {
            var query = this.Session.QueryOver<PurchaseInputReport>()
                .WhereRestrictionOn(p => p.DateInput).IsBetween(startDate).And(endDate);

            // Good
            if (reportType == 1)
            {
                query = query
                        .OrderBy(p => p.ItemId).Asc
                        .ThenBy(p => p.DateInput).Asc
                        .ThenBy(p => p.InputId).Asc
                        .ThenBy(p => p.OrderDate).Asc
                        .ThenBy(p => p.SupplierName).Asc;
            }
            else
            {
                query = query
                       .OrderBy(p => p.Supplierid).Asc
                       .ThenBy(p => p.DateInput).Asc
                       .ThenBy(p => p.InputId).Asc
                       .ThenBy(p => p.OrderDate).Asc
                       .ThenBy(p => p.ItemId).Asc;
            }

            if (!string.IsNullOrEmpty(supplierCode))
            {
                query = query.Where(p => p.Supplierid == supplierCode);
            }

            if (!string.IsNullOrEmpty(goodCode))
            {
                query = query.Where(p => p.ItemId == goodCode);
            }

            return query.List<PurchaseInputReport>();
        }

    }
}